package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import com.it2297.controller.CustomerController;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LoginView extends JFrame {

	private JPanel contentPane;
	private final CustomerController cc = new CustomerController(this);
	private final JLabel lblCustomerLogin = new JLabel("Login");
	private final JLabel lblUserName = new JLabel("User Name: ");
	private final JLabel lblPassword = new JLabel("Password: ");
	private final JButton btnLogin = new JButton("Login");
	private final JTextField login_TF = new JTextField();
	private String name;
	private String password;
	private final JPasswordField password_TF = new JPasswordField();
	private final JButton btnSignUp = new JButton("Sign Up");
	private final JLabel lblNewLabel = new JLabel("");
	private final JLabel lblCompanyLogin = new JLabel("<html><u>Click Here For Company Login</u></html>");
	private final JPanel panel = new JPanel();
	private final JLabel label = new JLabel("");
	private final JLabel off = new JLabel("");
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginView frame = new LoginView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginView() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 964, 623);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setTitle("Login Screen");
		login_TF.setBounds(309, 158, 234, 20);
		login_TF.setColumns(10);

		initGUI();
	}
	private void initGUI() {
		String currentWorkingDirectory = System.getProperty("user.dir");
		getContentPane().setLayout(null);
		contentPane.setLayout(null);
		
		off.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				int option = JOptionPane.showConfirmDialog (null, "Are you sure you want to close this application?",null, JOptionPane.YES_NO_OPTION);
				if (option == JOptionPane.YES_OPTION ) {
					close();
				}
				else{
					
				}
			}
		});
		off.setHorizontalAlignment(SwingConstants.CENTER);
		off.setBounds(770, 212, 33, 35);
		off.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/off.png"));
		contentPane.add(off);

		JLabel logo = new JLabel("");
		logo.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/logo.jpg"));
		logo.setBounds(155, 42, 129, 73);
		contentPane.add(logo);

		lblCustomerLogin.setForeground(Color.BLACK);
		lblCustomerLogin.setFont(new Font("Arial", Font.BOLD, 25));
		lblCustomerLogin.setBounds(309, 95, 111, 29);
		getContentPane().add(lblCustomerLogin);
		
		lblUserName.setForeground(Color.BLACK);
		lblUserName.setFont(new Font("Arial", Font.BOLD, 15));
		lblUserName.setBounds(309, 135, 111, 29);
		getContentPane().add(lblUserName);
		
		lblPassword.setForeground(Color.BLACK);
		lblPassword.setFont(new Font("Arial", Font.BOLD, 15));
		lblPassword.setBounds(309, 189, 84, 29);
		getContentPane().add(lblPassword);
		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(password_TF.getPassword().equals("")||login_TF.getText().equals("")){
					JOptionPane.showMessageDialog(null, "Please enter all the required details!");
				}
				else{
				cc.validateLogin();
				}
			}
		});
		btnLogin.setFont(new Font("Arial", Font.BOLD, 15));
		btnLogin.setBounds(309, 254, 89, 23);
		getContentPane().add(btnLogin);
		
		getContentPane().add(login_TF);
		
		password_TF.setBounds(309, 212, 234, 20);
		contentPane.add(password_TF);
		
		btnSignUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			CreateCustomerView ccv = new CreateCustomerView();
			ccv.setVisible(true);
			ccv.setLocationRelativeTo(null);
			}
		});
		btnSignUp.setFont(new Font("Arial", Font.BOLD, 15));
		btnSignUp.setBounds(426, 254, 117, 23);
		contentPane.add(btnSignUp);
		
		lblCompanyLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mouseEvent) {
				if(mouseEvent.getClickCount()>1){
					CompanyLogin cl  = new CompanyLogin();
					cl.setVisible(true);
					close();
				}
			}
		});
		lblCompanyLogin.setForeground(Color.BLUE);
		lblCompanyLogin.setFont(new Font("Arial", Font.BOLD, 15));
		lblCompanyLogin.setBounds(427, 42, 229, 20);
		contentPane.add(lblCompanyLogin);
	
		/*
		panel.setBackground(new Color(51, 204, 255));
		panel.setBounds(154, 34, 511, 301);
		
		contentPane.add(panel);
		*/
	
		JLabel img = new JLabel("");
		img.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/screen.jpg"));
		img.setBounds(0, 0, 948, 584);
		contentPane.add(img);
	}

	public String getNric() {
		return login_TF.getText();
	};
	
	public String getPassword(){
		String password = "";
		for(int i=0; i<password_TF.getPassword().length;i++){
			password += password_TF.getPassword()[i];
		}
		return password;
	}
	
	public void close(){
		this.dispose();
	}
}


